package n6_monitor对象头_偏向锁;


import lombok.extern.slf4j.Slf4j;
import org.openjdk.jol.info.ClassLayout;

/*----------------------------------------------
 *                                              |
 *      偏向锁 ===> 撤销 & 升级 轻量级锁           |
 *                                              |
 * =============================================*/
@Slf4j(topic = "c.T2_偏向锁_撤销")
public class T2_偏向锁_撤销 {

    /**
     * 1. 两个线程一起 运行
     * 2. T2线程 wait
     * 3. T1线程运行完，通知 T2
     *
     * 偏向锁是默认是延迟的，不会在程序启动时立即生效，如果想避免延迟，可以加 VM 参数  -XX:BiasedLockingStartupDelay=0
     *
     */
    public static void main(String[] args) throws InterruptedException {

        Dog d = new Dog();

        new Thread(()->{
            log.debug("T1 开始了 b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1b1");
            log.debug(ClassLayout.parseInstance(d).toPrintable());

            synchronized (d){

                log.debug(ClassLayout.parseInstance(d).toPrintable());
            }

            log.debug(ClassLayout.parseInstance(d).toPrintable());

            // 执行完 唤醒 锁 T2_偏向锁_撤销.class
            synchronized (T2_偏向锁_撤销.class){
                T2_偏向锁_撤销.class.notify();
            }

            log.debug("T1 结束了 end1end1end1end1end1end1end1end1end1end1end1end1end1end1end1end1end1end1end1end1end1end1end1end1end1end1end1end1end1end1end1end1end1end1end1end1end1end1end1end1");
        }, "t1").start();

        new Thread(()->{
            // 刚开始执行 就 wait
            synchronized (T2_偏向锁_撤销.class){
                try {
                    T2_偏向锁_撤销.class.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            log.debug("T2 开始了 b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2b2");

            log.debug(ClassLayout.parseInstance(d).toPrintable());

            synchronized (d){

                log.debug(ClassLayout.parseInstance(d).toPrintable());
            }

            log.debug(ClassLayout.parseInstance(d).toPrintable());
            log.debug("T2 结束了 end2end2end2end2end2end2end2end2end2end2end2end2end2end2end2end2end2end2end2end2end2end2end2end2end2end2end2end2end2end2end2end2end2end2end2end2end2end2end2end2");
        }, "t2").start();

    }

}


class Dog2 {

}



